

package mvc.manager
{
	import flash.utils.Dictionary;
	
	import mvc.IInitManagers;

	public class ManagerCenter implements IManagerCenter
	{
		static private var _instance:ManagerCenter;
		static public function get instance():ManagerCenter
		{
			if(!_instance)
			{
				new ManagerCenter;
			}
			
			return _instance;
		}
		
		private var _allMgrs:Dictionary = new Dictionary();
		
		private static const _msg:String = "register manager two times";
		
		public function ManagerCenter():void
		{
			if(_instance)
			{
				throw new Error("singleton error");
			}
			
			_instance = this;
		}
		
		public function registerManager(m:IManagerElement):void
		{
			var _t:IManagerElement = retrieveManager(m.managerName);
			if(_t)
			{
				throw new Error(_msg);
			}
			
			m.managerCenter = this;
			_allMgrs[m.managerName] = m;
		}
		
		public function retrieveManager(name:String):IManagerElement
		{
			return _allMgrs[name] as IManagerElement;
		}
		
		public function removeManager(name:String):IManagerElement
		{
			var _t:IManagerElement = retrieveManager(name);
			delete _allMgrs[name];
			
			return _t;
		}
		
		public function dispose():void
		{
		}
		
		public function initManagers(init:IInitManagers,data:Object):void
		{
			init.initManagers(this,data);
		}
	}
}